﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AjaxControlToolkit;
using WebRSSBuilder.Core; 
namespace WebRSSBuilder.ASP
{
    public partial class ViewRss : System.Web.UI.Page
    {
        private RSSObjectDataProvider rf; 
        protected void Page_Load(object sender, EventArgs e)
        {
          if (!IsPostBack)
            {
                if (Session["FileName"] == null)
                {
                    Response.Redirect("Default.aspx");
                }
                else
                if (!User.IsInRole(Session["Role"].ToString()))
                {
                    Session["ErrorNo"] = "1";
                    Response.Redirect("Error.aspx");
                }
                //Loading the XML file 
                //string filename = @"c:\users\sofian\documents\visual studio 2010\Projects\TestXML\WebApplication2\App_Data\news.xml";
                string filename = Session["FileURL"].ToString();
                this.Page.Title = "View " +Session["FileName"].ToString();
                try
                {
                    rf = new RSSObjectDataProvider(filename, new RSSFeedXLinqController());
                    Session["DataSource"] = rf;
                    BindData();
                   
                }
                catch (System.IO.FileNotFoundException exp) 
                {
                    Session["ErrorNo"] = "2";
                    Response.Redirect("Error.aspx");
                }
                catch (Exception ex)
                {
                    Session["ErrorMsg"] = ex.Message;
                    Response.Redirect("Error.aspx");
                }

             }
            else
            {
               rf = (RSSObjectDataProvider)Session["DataSource"] ;
            
            }
        }
        #region Helpers
        private void LoadDataProvider()
        {
            string filename = @"c:\users\sofian\documents\visual studio 2010\Projects\TestXML\WebApplication2\App_Data\news.xml";
            //string filename = Session["FileURL"].ToString();
            rf = new RSSObjectDataProvider(filename, new RSSFeedXLinqController());
            Session["DataSource"] = rf;

        }
        private void BindData()
        {
            this.TextBoxTitle.Text = rf.Title;
            this.TextBoxURL.Text = rf.WebURL;
            this.TextBoxCopyright.Text = rf.Copyright;
            this.TextBoxLanguage.Text = rf.Language;
            this.TextBoxEditor.Text = rf.Editor;
            this.TextBoxWebMaster.Text = rf.Webmaster;
            this.TextBoxDescription.Text = rf.Description;
            this.TextBoxSubject.Text = rf.Subject;
            this.TextBoxPubDate.Text = rf.PubDate.ToString("dddd MMMM d, yyyy");
            this.TextBoxLastBuild.Text = rf.LastBuildDate.ToString("dddd MMMM d, yyyy");
            this.TextBoxImageURL.Text = rf.ImgURL;
            this.TextBoxWidth.Text = rf.ImgTitle;
            this.TextBoxHeight.Text = rf.ImgLink;
            if (rf.StyleType == "text/xsl")
                this.DropDownListType.SelectedIndex = 0;
            else
                this.DropDownListType.SelectedIndex = 1;
        }
        protected void SaveChanges()
        {
            rf.Title = this.TextBoxTitle.Text;
            rf.WebURL = this.TextBoxURL.Text;
            rf.Copyright = this.TextBoxCopyright.Text;
            rf.Language = this.TextBoxLanguage.Text;
            rf.Editor = this.TextBoxEditor.Text;
            rf.Webmaster = this.TextBoxWebMaster.Text;
            rf.Description = this.TextBoxDescription.Text;
            rf.Subject = this.TextBoxSubject.Text;
            rf.PubDate = DateTime.Parse(this.TextBoxPubDate.Text);
            rf.LastBuildDate = DateTime.Parse(this.TextBoxLastBuild.Text);
            rf.ImgURL = this.TextBoxImageURL.Text;
            rf.ImgTitle = this.TextBoxWidth.Text;
            rf.ImgLink = this.TextBoxHeight.Text;
            rf.StyleType = this.DropDownListType.SelectedItem.Text;

        } 
        #endregion
        #region Object Data Source Events 
        protected void ObjectDataSource1_ObjectCreating(object sender, ObjectDataSourceEventArgs e)
        {
            e.ObjectInstance = this.rf;
        }

        protected void ObjectDataSource2_ObjectCreating(object sender, ObjectDataSourceEventArgs e)
        {

            e.ObjectInstance = this.rf;
        }

        protected void ObjectDataSource2_Inserting(object sender, ObjectDataSourceMethodEventArgs e)
        {
            SaveChanges();
        }

        protected void ObjectDataSource2_Deleting(object sender, ObjectDataSourceMethodEventArgs e)
        {
            SaveChanges();
        }

        protected void ObjectDataSource2_Updating(object sender, ObjectDataSourceMethodEventArgs e)
        {
            SaveChanges();
        }

        protected void ObjectDataSource2_Deleted(object sender, ObjectDataSourceStatusEventArgs e)
        {
            LoadDataProvider();
            this.GridView2.DataBind();
            this.DetailsView1.DataBind();
        }

        protected void ObjectDataSource2_Updated(object sender, ObjectDataSourceStatusEventArgs e)
        {
            LoadDataProvider();
            this.GridView2.DataBind();
            this.DetailsView1.DataBind();
        }

        protected void ObjectDataSource2_Inserted(object sender, ObjectDataSourceStatusEventArgs e)
        {
            LoadDataProvider();
            this.GridView2.DataBind();
            this.DetailsView1.DataBind();
        }

        protected void ButtonSave_Click(object sender, EventArgs e)
        {
            SaveChanges();
            this.rf.Save();
        }

        protected void ButtonCancel_Click(object sender, EventArgs e)
        {
            LoadDataProvider();
            BindData();

        } 
        #endregion
    }
}